package com.sam.im.sms.index.adapters;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.ContactsContract;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.sam.im.sms.R;
import com.sam.im.sms.entity.InterceptEntity;
import com.sam.im.sms.index.activitys.InterceptDetailsActivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

/**
 * 描述
 * liuyucheng
 * Created By 刘育成 on 2019/6/6.
 */
public class RefuseMailboxAdapter extends RecyclerView.Adapter<RefuseMailboxAdapter.ViewHolder> {
    private Context mContext;
    private List<InterceptEntity> mEntity;


    public RefuseMailboxAdapter(Context context, List<InterceptEntity> list) {
        mContext = context;
        mEntity = list;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.item_refuse_, viewGroup, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        InterceptEntity model = mEntity.get(position);
        viewHolder.bind(model, position);
    }

    @Override
    public int getItemCount() {
        return mEntity.size();
    }

    public class ViewHolder extends RecyclerView.ViewHolder {
        private TextView content;
        private int position;
        private TextView txtDateTime;
        private RelativeLayout rlTitle;
        private View vLine;
        private TextView name;
        private String[] contentProjection = new String[]{ContactsContract.PhoneLookup._ID,
                ContactsContract.PhoneLookup.DISPLAY_NAME};

        public ViewHolder(View itemView) {
            super(itemView);
            content = (TextView) itemView.findViewById(R.id.txt_date_title);
            rlTitle = (RelativeLayout) itemView.findViewById(R.id.rl_title);
            txtDateTime = (TextView) itemView.findViewById(R.id.txt_date_time);
            name = itemView.findViewById(R.id.name);
        }

        public void bind(InterceptEntity model, int position) {
            this.position = position;
            /**
             * 定义成局部变量，不要定义成成员变量
             */
            String phoneName = null;
            Uri uri = Uri.withAppendedPath(ContactsContract.PhoneLookup.CONTENT_FILTER_URI,
                    Uri.encode(model.getAddress()));
            Cursor concatCursor = mContext.getContentResolver().query(uri,
                    contentProjection, null, null, null);
            if (concatCursor.moveToFirst()) {
                // 查询到了联系人
                phoneName = concatCursor.getString(1);
            }
            concatCursor.close();

            if (phoneName != null) {
                name.setText(phoneName);
                Log.i("lyc", "phoneName:" + phoneName);
            } else {
                name.setText(model.getAddress());
                Log.i("lyc", "address:" + model.getAddress());
            }

            if (content != null && model.getBody().length() > 20) {
                content.setText(model.getBody().substring(0, 20));
            } else {
                content.setText(model.getBody());
            }

            Calendar calendar = Calendar.getInstance();
            calendar.setTimeInMillis(Long.valueOf(model.getDate()));

            SimpleDateFormat format = new SimpleDateFormat(
                    "MM/dd");
            String formatTime = format.format(calendar.getTime());
            txtDateTime.setText(formatTime);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Intent intent = new Intent(mContext, InterceptDetailsActivity.class);
                    intent.putExtra("phone", model.getAddress());
                    mContext.startActivity(intent);
                }
            });
        }
    }

}
